package com.ytjj.quartz.service;

import com.ytjj.qmyx.mall.mapper.DynamicExposureRecordMapper;
import com.ytjj.qmyx.mall.model.DynamicExposureRecord;
import com.ytjj.qmyx.mall.model.DynamicExposureRecordExample;
import com.ytjj.qmyx.users.mapper.UsersInfoMapper;
import com.ytjj.qmyx.users.model.UsersInfo;
import com.ytjj.qmyx.users.model.UsersInfoExample;
import com.ytjj.quartz.dao.UroraDao;
import com.ytjj.quartz.enums.DynamicExposureTypeEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UroraService {
    @Resource
    private UsersInfoMapper usersInfoMapper;
    @Resource
    private UroraDao uroraDao;
    @Resource
    private DynamicExposureRecordMapper dynamicExposureRecordMapper;

    /**
     * 查询达人的咨询量
     */
    public void calculationUroraNum() {
        UsersInfoExample usersInfoExample = new UsersInfoExample();
        usersInfoExample.createCriteria().andIsQualityEqualTo(1);
        List<UsersInfo> usersInfoList = usersInfoMapper.selectByExample(usersInfoExample);
        if (CollectionUtils.isEmpty(usersInfoList)) {
            return;
        }
        Map<String, Object> result = curDay();
        Date startTime = (Date) result.get("dayStart");
        Date endTime = (Date) result.get("dayEnd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        usersInfoList.stream().forEach(item -> {
            Integer num = 0;
            List<Integer> list = uroraDao.sessionNum(item.getId());
            if (!CollectionUtils.isEmpty(list)){
                num = list.size();
            }
            //查询当天是否有数据,最新一条数据
            DynamicExposureRecordExample dynamicExposureRecordExample = new DynamicExposureRecordExample();
            dynamicExposureRecordExample.createCriteria()
                    .andUserIdEqualTo(item.getId())
                    .andTypeEqualTo(DynamicExposureTypeEnums.URORA.getCode().byteValue())
                    .andCreateTimeBetween(startTime,endTime);
            dynamicExposureRecordExample.setOrderByClause("create_time desc limit 1");
            List<DynamicExposureRecord> dynamicExposureRecords = dynamicExposureRecordMapper.selectByExample(dynamicExposureRecordExample);
            if (!CollectionUtils.isEmpty(dynamicExposureRecords)){
                if (dynamicExposureRecords.get(0).getTaskValue().equals(num)){
                    return;
                }
                DynamicExposureRecord record = new DynamicExposureRecord();
                record.setId(dynamicExposureRecords.get(0).getId());
                record.setTaskValue(num);
                int count = dynamicExposureRecordMapper.updateByPrimaryKeySelective(record);
                if (0 == count ) {
                    log.warn("===达人咨询量更新异常==={}", item.getId());
                }
            }else {
                DynamicExposureRecord record = new DynamicExposureRecord();
                record.setCreateTime(new Date());
                record.setUserId(item.getId());
                record.setType(DynamicExposureTypeEnums.URORA.getCode().byteValue());
                record.setTaskValue(num);
                int count = dynamicExposureRecordMapper.insertSelective(record);
                if (0 == count ) {
                    log.warn("===达人咨询量保存异常==={}", item.getId());
                }
            }
        });
    }

    /**
     * 今天开始时间到当前时间
     * @return
     */
    public static Map<String, Object> curDay() {
        LocalDate ld = LocalDate.now(ZoneId.systemDefault());
        LocalDateTime localDateTime = ld.atStartOfDay();

        Date startTime = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

        LocalDateTime ldt_end = LocalDateTime.now(ZoneId.systemDefault());
        Date endTime = Date.from(ldt_end.atZone(ZoneId.systemDefault()).toInstant());

        Map<String, Object> result = new HashMap<>();
        result.put("dayStart", startTime);
        result.put("dayEnd", endTime);
        return result;
    }
}
